package com.mantbox.channelcall.controller;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import com.mantbox.channelcall.entity.Report;
import com.mantbox.channelcall.entity.UrlLink;
import com.mantbox.channelcall.service.ReportService;
import com.mantbox.channelcall.service.UrlService;
import com.mantbox.channelcall.utils.DatabaseUtil;
import com.mantbox.channelcall.utils.ResResult;
import com.mantbox.channelcall.utils.UrlUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/url")
public class UrlController {

    @Autowired
    UrlService urlService;

    @Autowired
    ReportService reportService;

    @Autowired
    UrlUtils urlUtils;

    @Autowired
    DatabaseUtil databaseUtil;


    /*
    * pageList: 查询列表
    * page：页码
    * pageSize：每页数据
    * usePageMode：0 不适用分页，1 分页
    * groupId：分组
    * */
    @RequestMapping("/pageList")
    public ResResult pageList(Integer page,Integer pageSize,Integer usePageMode, Integer groupId,String keyword) {
        PageInfo<UrlLink> pageInfo = urlService.pageList(page,pageSize,groupId,usePageMode);
        return ResResult.isOk("成功",pageInfo.getList());
    }

    /*
    * 更新链接状态
    * map：链接信息
    * */
    @PostMapping("/updateUrlStatus")
    public ResResult updateUrlStatus(@RequestBody Map<String,Object> map) {
        String listStr = JSON.toJSONString(map.get("list"));
        List<UrlLink> urlLinks = JSON.parseArray(listStr,UrlLink.class);
//        urlService.batchUpdateUrlStatus(urlLinks);
        databaseUtil.updateUrlStatus(urlLinks);
        return ResResult.isOk("","");
    }

    @GetMapping("/generateReportUrl")
    public ResResult generateReportUrl(@RequestParam(required = true) String urlId,@RequestParam(required = false) String idfa) {
        return ResResult.isOk("",urlUtils.generateUrlByUrlId(urlId,idfa));
    }
}
